package com.llbnk.springcoding.util;


import com.alibaba.fastjson.JSON;
import com.llbnk.springcoding.thread.ChangeCapacityBlockingQueue;
import com.llbnk.springcoding.thread.ThreadExcutorFactory;
import com.llbnk.springcoding.thread.ThreadExcutorWrapper;
import com.mysql.cj.log.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Function;


@Slf4j
public class ThreadExcutorUtils {
    
    public static <T> T convert(String value, Function<String,T> function, T defaultValue){
        T apply = defaultValue;
        try {
            apply = function.apply(value);
        }catch (Exception e){
            log.error("convert thread pool config happen error,value:{},defaultValue:{}",value,defaultValue);
        }
        return apply;
    }


    public static void refreshReportExcutor(String poolName) {
        ThreadExcutorWrapper wrapper = ThreadExcutorFactory.getThreadExcutor(poolName);
        if(Objects.isNull(wrapper)){
            return ;
        }
        ThreadPoolExecutor executor = (ThreadPoolExecutor) wrapper.getExecutorService();
        ChangeCapacityBlockingQueue<?> queue = (ChangeCapacityBlockingQueue<?>) executor.getQueue();

        log.info("线程池:{} \n" +
                        "核心线程数:{} \n" +
                        "最大线程数:{} \n" +
                        "队列长度:{} \n",
                poolName,
                executor.getCorePoolSize(),
                executor.getMaximumPoolSize(),
                queue.getCapacity());
    }
}
